package br.com.mundoj.subscription.client.services;

/*
 * Security and Cross-Site Requests
 * Before you implement mashups of your own, remember that downloading cross-site JSON is powerful, 
 * but can also be a security risk. Make sure the servers you interact with are absolutely trustworthy, 
 * because they will have the ability to execute arbitrary JavaScript code within your application. 
 * Take a minute to read "Security for GWT Applications", which describes the potential threats to 
 * GWT applications and how you can combat them.
 */
public class CepServiceRepublicaVirtualImpl implements CepService {
	CepServiceRepublicaVirtualImpl() {
		init();
	};
	
	public native void clear(Void v)  /*-{
		resultadoCEP = null;
		var script = window[callback];
		if (script!=null) {
			clearInterval(window[timerId]);
			clearTimeout(window[timeoutId]);
			delete window[timer];
			delete window[timerId];
			delete window[timeout];
			delete window[timeoutId];
			delete window[callback];
			document.body.removeChild(script);
		}
	}-*/;
	
	public native void init() /*-{
		callback = "cepCallback";
		timer = "cepCallback-timer";
		timerId = "cepCallback-timer-id";
		timeout = "cepCallback-timeout";
		timeoutId = "cepCallback-timeout-id";
	}-*/;
	
	public native void execute(String cep, int timeoutInMiliseconds, Callback cb) /*-{
		// [1] checks pendent requests and cleanup
		this.@br.com.mundoj.subscription.client.services.CepServiceRepublicaVirtualImpl::clear(Ljava/lang/Void;)(null);
		
		// [2] Create a script element and stores on window object.
		script = document.createElement("script");
		window[callback] = script;

		var THIS = this;
		
		// [3A] Define a timer function for JSONP request, to check if resultadoCEP was answered 
		window[timer] = function() {
			// [4A] Calling Java methods from JavaScript is somewhat similar 
			// to calling Java methods from C code in JNI. 
			// In particular, JSNI borrows the JNI mangled method signature approach 
			// to distinguish among overloaded methods.
			if (resultadoCEP!=null) {
				cb.@br.com.mundoj.subscription.client.services.CepService.Callback::execute(Lbr/com/mundoj/subscription/client/ILocationData;)(resultadoCEP);
				THIS.@br.com.mundoj.subscription.client.services.CepServiceRepublicaVirtualImpl::clear(Ljava/lang/Void;)(null);				
			}
		}

		// [3A] Define a timeout function for JSONP request
		window[timeout] = function() {
			// [4A] Calling Java methods from JavaScript is somewhat similar 
			// to calling Java methods from C code in JNI. 
			// In particular, JSNI borrows the JNI mangled method signature approach 
			// to distinguish among overloaded methods.
			cb.@br.com.mundoj.subscription.client.services.CepService.Callback::execute(Lbr/com/mundoj/subscription/client/ILocationData;)(resultadoCEP);
			THIS.@br.com.mundoj.subscription.client.services.CepServiceRepublicaVirtualImpl::clear(Ljava/lang/Void;)(null);			
		}

		// otherwise, bridges to Java code with an error message
		
		// [5] Setup script loading 
		var url = "http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + cep;
		script.setAttribute("src", url);
		script.setAttribute("type", "text/javascript");
		
		// [6] Attach the script element to the document body.
		document.body.appendChild(script);
		
		// [7] Set timer and timeout functions 
		window[timerId] = setInterval(window[timer], 100);
		window[timeoutId] = setTimeout(window[timeout], timeoutInMiliseconds);
	}-*/;
}